package com.baichang.taoshunda.service.impl;

import com.baichang.framework.utils.http.HttpUtils;
import com.baichang.taoshunda.entity.Token;
import com.baichang.taoshunda.service.TrendService;
import com.baichang.taoshunda.utils.TrendsConfigConstants;
import com.google.gson.Gson;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2018\9\16 0016.
 */
@Service
public class TrendServiceImpl implements TrendService {
    @Override
    public String saveUser(String account, String pwd, Integer userId, String nickName, String phone, String headPic, int userType) {
        String saveUser = TrendsConfigConstants.SAVE_USER_IP;
        JSONObject userObject = new JSONObject();
        userObject.put("userId", userId);
        userObject.put("userName", account);
        userObject.put("password", pwd);
        userObject.put("phoneNumber", phone);
        userObject.put("userType", userType);
        userObject.put("nickName", nickName);
        userObject.put("headPicture", headPic);
        userObject.put("sex", " ");
        System.out.println("===" + userObject.toString());
        String post = HttpUtils.postJson(saveUser, userObject.toString());
        return post.trim();
    }


    @Override
    public String updateUser(String account, String pwd, Integer userId, String nickName, String phone, String headPic, int userType) {
        String updateUser = TrendsConfigConstants.UPDATE_INFO_IP;
        JSONObject userObject = new JSONObject();
        userObject.put("userId", userId);
        userObject.put("userName", account);
        userObject.put("password", pwd);
        userObject.put("phoneNumber", phone);
        userObject.put("userType", userType);
        userObject.put("nickName", nickName);
        userObject.put("headPicture", headPic);
        userObject.put("sex", " ");
        System.out.println("===" + userObject.toString());
        String post = HttpUtils.postJson(updateUser, userObject.toString());
        return post.trim();
    }

    @Override
    public JSONObject getToken(Integer userId, int userType) {
        String getToken = TrendsConfigConstants.GET_TOKEN_IP;
        JSONObject userObject = new JSONObject();
        userObject.put("userId", userId);
        userObject.put("userType", userType);
        System.out.println("===" + userObject.toString());
        String post = HttpUtils.postJson(getToken, userObject.toString());
        System.out.println(post);
        JSONObject jsonObject = JSONObject.fromObject(post.trim());
        return jsonObject;
    }

    @Override
    public String getUserId(String string) {
        String getUserId = TrendsConfigConstants.GET_USER_ID_IP;
        String post = null;
        try {
            Token token = new Token();
            token.setToken(string.trim());
            Gson gson = new Gson();
            System.out.println(gson.toJson(string));
            post = HttpUtils.postJson(getUserId, gson.toJson(token));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return post.trim();
    }

    @Override
    public String updateInfo(int userId, String account, String password, String phoneNumber, int userType, String nickName, String headPic, String sex) {
        String update = TrendsConfigConstants.UPDATE_INFO_IP;
        JSONObject userObject = new JSONObject();
        userObject.put("userId", userId + "");
        userObject.put("userName", account);
        userObject.put("password", password);
        userObject.put("phoneNumber", phoneNumber);
        userObject.put("userType", userType);
        userObject.put("nickName", nickName);
        userObject.put("headPicture", headPic);
        userObject.put("sex", sex);
        System.out.println(userObject.toString());
        String post = HttpUtils.postJson(update, userObject.toString());
        return post.trim();
    }

    @Override
    public String updateShare(Integer shareId, String comment, BigDecimal oldPrice, BigDecimal nowPrice, String synopsis, String address, Integer type) {
        String update = TrendsConfigConstants.UPDATE_GOODS_IP;
        JSONObject userObject = new JSONObject();
        userObject.put("shareId", shareId);
        userObject.put("comment", comment);
        userObject.put("oldPrice", oldPrice);
        userObject.put("nowPrice", nowPrice);
        userObject.put("synopsis", synopsis);
        userObject.put("address", address);
        userObject.put("type", type);
        System.out.println(userObject.toString());
        String post = HttpUtils.postJson(update, userObject.toString());
        return post.trim();
    }

    @Override
    public String updateStatusById(String shareId) {
        String update = TrendsConfigConstants.updateStatusById;
        System.out.println("============================" + shareId);
        System.out.println(update);
        String post = HttpUtils.post(update + shareId);
        System.out.println(post);
        return post.trim();
    }


}
